/**
 * AjLog - The AspectJ based logging facade
 *
 * Copyright (C) 2009 - 2010, Marko Umek. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajlog.core.logging.internal;

import org.ajlog.annotations.levels.EAjLogLevel;
import org.ajlog.core.logging.AbstractLogMethod;
import org.ajlog.core.logging.AbstractLoggingFramework;
import org.ajlog.core.logging.AjLogLevelMapper;
import org.ajlog.core.logging.IAjLogLevelMapper;
import org.ajlog.core.logging.ILogEntry;
import org.ajlog.core.logging.ILoggingFramework;
import org.ajlog.loglog.AjLogLogger;
import org.ajlog.loglog.ILogger;

/**
 * If no logging framework has been defined, the DefaultLoggingFramework will be
 * used.
 * 
 * @author Marko Umek
 * 
 */
public final class DefaultLoggingFramework extends AbstractLoggingFramework<ILogger>
    implements ILoggingFramework {

    /**
     * Constructor
     * 
     */
    public DefaultLoggingFramework() {
    }

    /* (non-Javadoc)
     * @see org.ajlog.core.logging.internal.AbstractLoggingFramework#doSetupLevelMapper(org.ajlog.core.logging.AjLogLevelMapper)
     */
    @Override
    protected IAjLogLevelMapper doSetupLevelMapper(final AjLogLevelMapper<ILogger> ajLogLevelMapper) {
        // FilterLevel.DEBUG, FilterLevel.TRACE
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.TRACE,
                new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isTraceEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.trace(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.trace(logEntry.resolveMessage(), exception);
                    }
                }
                );
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.DEBUG,
                new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isDebugEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.debug(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.debug(logEntry.resolveMessage(), exception);
                    }
                }
                );
        // FilterLevel.INFO
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.INFO, new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isInfoEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.info(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.info(logEntry.resolveMessage(), exception);
                    }
                }
                );
        // FilterLevel.WARNING
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.WARNING, new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isWarningEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.warning(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.warning(logEntry.resolveMessage(), exception);
                    }
                }
                );
        // FilterLevel.ERROR
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.ERROR,
                new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isErrorEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.error(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.error(logEntry.resolveMessage(), exception);
                    }
                }
                );
        // FilterLevel.ERROR
        ajLogLevelMapper.addLogMethodPrototype(
                 EAjLogLevel.SEVERE,
                 new AbstractLogMethod<ILogger>() {
                     public boolean isEnabled() {
                         return this.logger.isSevereEnabled();
                     };

                     @Override
                     protected void doLogWithoutException(final ILogEntry logEntry) {
                         this.logger.severe(logEntry.resolveMessage());
                     }

                     @Override
                     protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                         this.logger.severe(logEntry.resolveMessage(), exception);
                     }
                 }
                );
        // FilterLevel.FATAL
        ajLogLevelMapper.addLogMethodPrototype(
                EAjLogLevel.FATAL, new AbstractLogMethod<ILogger>() {
                    public boolean isEnabled() {
                        return this.logger.isSevereEnabled();
                    };

                    @Override
                    protected void doLogWithoutException(final ILogEntry logEntry) {
                        this.logger.fatal(logEntry.resolveMessage());
                    }

                    @Override
                    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
                        this.logger.fatal(logEntry.resolveMessage(), exception);
                    }
                }
                );
        return ajLogLevelMapper;
    }

    /* (non-Javadoc)
     * @see org.ajlog.core.logging.internal.AbstractLoggingFramework#doCreateLoggingInstance(java.lang.String)
     */
    @Override
    protected ILogger doCreateLoggingInstance(final String name) {
        return AjLogLogger.getLogger(name);
    }
}
